﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvc._5Dev.SettlementExpenses.ServiceReferenceSettlementExpenses;
namespace Mvc._5Dev.SettlementExpenses.Controllers
{
    public class DailyExpenseController : Controller
    {
        //
        // GET: /DailyExpenses/
        private  List<DailyExpense> ListDailyExpense()
        {
            var oListDailyExpense = new List<DailyExpense>();

            var oDailyExpense = new DailyExpense { Id = 1, CreateDate = DateTime.Now, Mount = 110 };


            var oListCostConcept = new List<CostConcept>
                                                     {
                                                         new CostConcept()
                                                             {Id = 1, Code = "Concept 1", Description = "Concept 1",IsSelected = true},
                                                         new CostConcept()
                                                             {Id = 2, Code = "Concept 2", Description = "Concept 2",IsSelected = true},
                                                         new CostConcept()
                                                             {Id = 2, Code = "Concept 3", Description = "Concept 3"},
                                                         new CostConcept()
                                                             {Id = 2, Code = "Concept 4", Description = "Concept 4"}
                                                     };

            var oListEmployee = new List<Employee>
                                    {
                                        new Employee() {Id = 1, Name = "Robert Wong Villar"},
                                        new Employee() {Id = 2, Name = "Juan Carlos Medina"},
                                        new Employee() {Id = 3, Name = "Carlos Carrillo"}
                                    };


            oDailyExpense.CostConcepts = oListCostConcept.ToArray();
            oDailyExpense.Employees = oListEmployee.ToArray();

            oListDailyExpense.Add(oDailyExpense);

            return oListDailyExpense;
        }

        private DailyExpense GetDailyExpense(int id)
        {
            var oListDailyExpense = (List<DailyExpense>)Session["oListDailyExpense"];
            var model=oListDailyExpense.Single(item => item.Id == id);
            return model;
        }

        SettlementExpensesClient oSettlementExpensesClient = new SettlementExpensesClient();
        public ActionResult Index()
        {
            //return View(oSettlementExpensesClient.ListDailyExpense());

            if (Session["oListDailyExpense"] == null)
                Session["oListDailyExpense"] = ListDailyExpense();

            var model = (List<DailyExpense>) Session["oListDailyExpense"];
            return View(model);
        }
        public ActionResult Create()
        {
            return View(ListDailyExpense()[0]);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection formCollection)
        {
            //string title = formCollection.Get("Title");
            //string body = formCollection.Get("Body");
            //bool isPublic = formCollection.Get("IsPublic").Contains("true");

            //IRepository<Category> categoriesRepo = new CategoryRepository();
            //IRepository<DailyExpense> DailyExpensesRepo = new DailyExpenseRepository();
            //List<Category> allCategories = (List<Category>)categoriesRepo.GetAll();

            //DailyExpense DailyExpense = new DailyExpense();
            //DailyExpense.Body = body;
            //DailyExpense.Title = title;
            //DailyExpense.CreationDate = DateTime.Now;
            //DailyExpense.IsPublic = isPublic;

            //foreach (Category category in allCategories)
            //{
            //    if (formCollection.Get(category.Id.ToString()).Contains("true"))
            //        DailyExpense.Categories.Add(category);

            //}

            //DailyExpensesRepo.Save(DailyExpense);


            return RedirectToAction("Index");
        }

        public ActionResult Delete(Guid id)
        {
            //IRepository<DailyExpense> repo = new DailyExpenseRepository();
            //repo.Delete(repo.GetById(id));
            return RedirectToAction("Index");
        }

        public ActionResult Details(int id)
        {
            DailyExpense model = GetDailyExpense(id);
            return View(model);
            //return View(oSettlementExpensesClient.GetDailyExpense(id));
        }

        public ActionResult Edit(int id)
        {
            //IRepository<DailyExpense> DailyExpensesRepo = new DailyExpenseRepository();
            //IRepository<Category> categoriesRepo = new CategoryRepository();

            //DailyExpenseViewModel model = new DailyExpenseViewModel(DailyExpensesRepo.GetById(id), categoriesRepo.GetAll());
            DailyExpense model = GetDailyExpense(id);
            return View(model);
            //return View(oSettlementExpensesClient.GetDailyExpense(id));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Guid id, FormCollection formCollection)
        {
            //string title = formCollection.Get("Title");
            //string body = formCollection.Get("Body");
            //bool isPublic = formCollection.Get("IsPublic").Contains("true");

            //IRepository<Category> categoriesRepo = new CategoryRepository();
            //IRepository<DailyExpense> DailyExpensesRepo = new DailyExpenseRepository();
            //List<Category> allCategories = (List<Category>)categoriesRepo.GetAll();

            //DailyExpense DailyExpense = new DailyExpense();
            //DailyExpense.Body = body;
            //DailyExpense.Title = title;
            //DailyExpense.IsPublic = isPublic;
            //DailyExpense.Id = id;

            //foreach (Category category in allCategories)
            //{
            //    if (formCollection.Get(category.Id.ToString()).Contains("true"))
            //        DailyExpense.Categories.Add(category);
            //}

            //DailyExpensesRepo.Update(DailyExpense);

            return RedirectToAction("Index");
        }


    }
}
